package com.ktjy.ktmall.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjy.ktmall.domain.Attr;
import com.ktjy.ktmall.service.IAttrService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktjy.common.annotation.Log;
import com.ktjy.common.core.controller.BaseController;
import com.ktjy.common.core.domain.AjaxResult;
import com.ktjy.common.enums.BusinessType;
import com.ktjy.ktmall.domain.AttrValues;
import com.ktjy.ktmall.service.IAttrValuesService;
import com.ktjy.common.utils.poi.ExcelUtil;
import com.ktjy.common.core.page.TableDataInfo;

/**
 * 规格属性值Controller
 *
 * @author ktjy
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/ktmall/values")
public class AttrValuesController extends BaseController
{
    @Autowired
    private IAttrValuesService attrValuesService;

    @Autowired
    private IAttrService attrService;


    /**
     * 属性值列表
     */
//    @PreAuthorize("@ss.hasPermi('ktmall:values:attrList')")
    @GetMapping("/attrList")
    public TableDataInfo attrList() {
        startPage();
        List<Attr> list = attrService.list();
        return getDataTable(list);
    }


/**
 * 查询规格属性值列表
 */
@PreAuthorize("@ss.hasPermi('ktmall:values:list')")
@GetMapping("/list")
    public TableDataInfo list(AttrValues attrValues)
    {
        startPage();
        List<AttrValues> list = attrValuesService.selectAttrValuesList(attrValues);
        return getDataTable(list);
    }


    /**
     * 导出规格属性值列表
     */
    @PreAuthorize("@ss.hasPermi('ktmall:values:export')")
    @Log(title = "规格属性值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttrValues attrValues)
    {
        QueryWrapper<AttrValues> queryWrapper = new QueryWrapper<>();
        if (attrValues.getAttrId()!=null){
            queryWrapper.eq("attr_id",attrValues.getAttrId());
        }
        if (attrValues.getName()!=null){
            queryWrapper.like("name",attrValues.getName());
        }
        List<AttrValues> list = attrValuesService.list(queryWrapper);
        ExcelUtil<AttrValues> util = new ExcelUtil<AttrValues>(AttrValues.class);
        util.exportExcel(response, list, "规格属性值数据");
    }

    /**
     * 获取规格属性值详细信息
     */
    @PreAuthorize("@ss.hasPermi('ktmall:values:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attrValuesService.getById(id));
    }

    /**
     * 新增规格属性值
     */
    @PreAuthorize("@ss.hasPermi('ktmall:values:add')")
    @Log(title = "规格属性值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttrValues attrValues)
    {
        return toAjax(attrValuesService.save(attrValues));
    }

    /**
     * 修改规格属性值
     */
    @PreAuthorize("@ss.hasPermi('ktmall:values:edit')")
    @Log(title = "规格属性值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttrValues attrValues)
    {
        return toAjax(attrValuesService.updateById(attrValues));
    }

    /**
     * 删除规格属性值
     */
    @PreAuthorize("@ss.hasPermi('ktmall:values:remove')")
    @Log(title = "规格属性值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Integer> ids)
    {
        return toAjax(attrValuesService.removeByIds(ids));
    }
}